from Products.CMFCore.utils import getToolByName
from Products.CMFPlone.utils import base_hasattr
from plone.app.users.browser.register import RegistrationForm
from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile
import urllib
class Login:
    def __init__(self,context,request):
        self.context = context
        self.request = request
    def __call__(self):
        descDict = {}
        if base_hasattr(self.request, '__ac_name') and base_hasattr(self.request, '__ac_password'):
            mt = getToolByName(self.context, 'portal_membership')
            # acl_users = getToolByName(self.context, 'acl_users')
            username = self.request.get('__ac_name','').lower()
            
            if mt.isAnonymousUser() or mt.getAuthenticatedMember().getUserName() != username: # the user has not logged in
                descDict['Status'] = 'Error'
                descDict['Message'] = 'LoginError'
                self.request.RESPONSE.expireCookie('__ac', path='/')
            else:
                try:
                    result = mt.testCurrentPassword(self.request.get('__ac_password','')) 
                    if result == None:
                        descDict['Status'] = 'Error'
                        descDict['Message'] = 'LoginError'
                        self.request.RESPONSE.expireCookie('__ac', path='/')
                    else:
                        mt.loginUser(self.request)
                        descDict['Status'] = 'Success'
                        cookieAuth = self.request.RESPONSE.cookies['__ac']['value']
                        cookieAuth = unicode(cookieAuth,"gbk")
                        cookieAuth = cookieAuth.encode("utf-8")
                        cookieAuth = urllib.quote(cookieAuth)
                        descDict['Message'] =  '%s' % cookieAuth
                        self.request.RESPONSE.expireCookie('__ac', path='/')
                except:
                    descDict['Status'] = 'Error'
                    descDict['Message'] = 'LoginError'
                    self.request.RESPONSE.expireCookie('__ac', path='/')
                
                    
        else:
            descDict['Status'] = 'Error'
            descDict['Message'] = 'LackData'

        result = "{\"Status\":\"%s\",\"Message\":\"%s\"}" % (descDict['Status'],descDict['Message'])
        return result
    
class CheckCookies:
    def __init__(self,context,request):
        self.context = context
        self.request = request
    def __call__(self):
        descDict = {}
        mt = getToolByName(self.context, 'portal_membership')
        
        if mt.isAnonymousUser(): # the user has not logged in
                descDict['Status'] = 'Error'
                descDict['Message'] = 'LoginError'
        else:
            descDict['Status'] = 'Success'
            descDict['Message'] = "Success"
        result = "{\"Status\":\"%s\",\"Message\":\"%s\"}" % (descDict['Status'],descDict['Message'])
        
        return result
    
class Register(RegistrationForm):
    """Register
    """
    template = ViewPageTemplateFile('register_form.pt')
    